<?php
    include(dirname(__FILE__)."/../../../Dao/db.php");
    include(dirname(__FILE__)."/../../../domain_url.php");

    class CreativeService
    {
        var $memberId;
        //キャンペーンID
        var $camp_Id;
        //キャンペーン名
        var $campaignName;
        //広告グループID
        var $adGr_Id;
        //広告グループ名
        var $adGroupName;
        //クリエイティブID
        var $Crt_Id;
        //クリエイティブ名
        var $creativeName;
        //タイトル
        var $title;
        //短縮説明１
        var $description1;
        //短縮説明２
        var $description2;
        //表示URL
        var $displayUrl;
        //ランディング先URL
        var $destinationUrl;
        //ステータス
        var $status;
        //ドメインＵＲＬ
        var $domain;

        function CreativeService()
        {
            session_start();
            $this->memberId = $_SESSION['memberId'];
            $this->camp_Id = $_GET['campaignId'];
            $this->campaignName = $this->getCampaignName($this->camp_Id);//$_GET['campaignName'];
            $this->adGr_Id = $_GET['adGroupId'];
            $this->adGroupName = $this->getAdGroupName($this->adGr_Id);//$_GET['adgroupName'];
            $this->Crt_Id = $_GET['creativeId'];
            $this->creativeName = $_POST['creative_name'];
            $this->title = $_POST['title'];

            $descriptions = split("\r\n",$_POST['short_description']);
            $this->description1 = $descriptions[0];
            $this->description2 = $descriptions[1];

            $this->displayUrl = $_POST['display_url'];
            $this->destinationUrl = $_POST['landing_url_creative'];
            $this->status = $_POST['status'];

            $this->domain = DOMAIN_URL."?Crt_Id=";
        }

        function getCampaignName($camp_Id)
        {
            $query = "SELECT  name ";
            $query .= "FROM   campaign ";
            $query .= "WHERE  camp_Id=$camp_Id";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            return $row['name'];
        }

        function getAdGroupName($adGr_Id)
        {
            $query = "SELECT  name ";
            $query .= "FROM   adgroup ";
            $query .= "WHERE  adGr_Id=$adGr_Id";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            return $row['name'];
        }

        //同期エラーを表示する
        function getErrorMessage()
        {
            $query = "SELECT syncError FROM creative ";
            $query .= "WHERE Crt_Id=$this->Crt_Id ";
            $query .= "AND sync=3 ";

            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            $syncError = trim($row['syncError']);
            if($syncError)
            {
                $errorTable = "<br><table class='tbl' width='100%' border='0' cellpadding='4' cellspacing='0'>";
                $errorTable .= "<tbody><tr>";
                $errorTable .= "<td class='td9' width='150' align='left'>";
                $errorTable .= "<img src='../images/sync_error.gif' border='0'>エラー</td>";
                $errorTable .= "<td class='td4 left'><pre>".$syncError."</pre></td>";
                $errorTable .= "</tr></tbody>";
                $errorTable .= "</table><br>";
            }

            echo $errorTable;
        }

        function addCreative()
        {
            $now = date("Y-m-d H:i:s");
            //メディア種類を取得
            $query = "SELECT listingType FROM campaign ";
            $query .= "WHERE camp_Id=$this->camp_Id ";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            $listingType = $row['listingType'];

            $query = "INSERT INTO creative(memberId,listingType,camp_Id,adGr_Id,";
            $query .= "name,headline,description1,description2,";
            $query .="displayUrl,destinationUrl,status,sync,registDate,lastUpdate) ";
            $query .= "VALUES($this->memberId,";
            $query .= "$listingType,";
            $query .= "$this->camp_Id,";
            $query .= "$this->adGr_Id,";
            $query .= "'$this->creativeName',";
            $query .= "'$this->title',";
            $query .="'$this->description1',";
            $query .="'$this->description2',";
            $query .= "'$this->displayUrl',";
            $query .= "'$this->destinationUrl',";
            $query .= "$this->status,";
            //Googleに未同期
            $query .= "2,";
            $query .= "'$now','$now')";

            $result = mysql_query($query);
            if ($result != "1")
            {
                $message = "登録失敗しました";
            }
            else
            {
                $query = "update creative set redirectUrl=CONCAT('$this->domain',Crt_Id) ";
                $query .= " where adGr_Id=$this->adGr_Id and lastUpdate='$now'";
                mysql_query($query);
                $message = "登録しました";
            }

            

            return $message;
        }

        function updateCreative()
        {
            $query = "UPDATE creative SET name='$this->creativeName',";
            $query .= "headline='$this->title',";
            $query .= "description1='$this->description1',";
            $query .= "description2='$this->description2',";
            $query .= "displayUrl='$this->displayUrl',";
            $query .= "destinationUrl='$this->destinationUrl',";
	    $query .= "redirectUrl=CONCAT('$this->domain',Crt_Id),";
            $query .= "status=$this->status,";
            //更新待ち
            $query .= "sync=2,lastUpdate=now()";
            $query .= "WHERE Crt_Id=$this->Crt_Id";
            $result = mysql_query($query);

            if ($result != "1")
            {
                $message = "更新失敗しました";
            }
            else
            {
                $message = "更新しました";
            }
            return $message;

        }

        function updateAllCreatives()
        {
            $creativeIds = split(",",$_GET['creativeIds']);

             $ids = "(";
            for($i=0;$i<count($creativeIds)-1;$i++)
            {
                $ids .=$creativeIds[$i].",";
            }
            $ids .= $creativeIds[count($creativeIds)-1].")";

            $queryHead = "UPDATE creative SET ";

            if(isset($_POST['change_status']))
            {
                $queryBody = "status=$this->status,";
            }

            if(isset($_POST['change_disp']))
            {
                $queryBody .= "displayUrl='$this->displayUrl',";
            }

            if(isset($_POST['change_url']))
            {
                $queryBody .= "destinationUrl='$this->destinationUrl',";
            }

            if(isset($_POST['change_delete']))
            {
                //この状態の意味は削除された
                $queryBody .= "status=9,";
            }

            if(isset($queryBody))
            {
                $query = $queryHead.$queryBody;
                //更新待ち
                $query .= "sync=2 WHERE ";
                $query .= "Crt_Id IN $ids";

                $result = mysql_query($query);
                if ($result != "1")
                {
                    $message = "更新失敗しました";
                    return $message;
                }
                else
                {
                    $message = "更新しました";
                }
            }



            return $message;
        }



    }
?>
